package cn.edu.neusoft.maxingxin231.yuejian06.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;


import cn.edu.neusoft.maxingxin231.yuejian06.Bean.LoginBean;
import cn.edu.neusoft.maxingxin231.yuejian06.Listener.LoginListener;
import cn.edu.neusoft.maxingxin231.yuejian06.Model.LoginModel;
import cn.edu.neusoft.maxingxin231.yuejian06.R;

public  class LoginActivity extends BaseActivity{
    EditText ed_user,ed_pw;
    Button btn_login,btn_register;
    CheckBox checkBox;
    String user_phone,user_password,uid;
    LoginListener<LoginBean>loginListener=new LoginListener<LoginBean>() {
        @Override
        public void onResponse(LoginBean loginBean) {
            if (loginBean.getUid().equals("0")){
                showToast("登陆失败");
            }
            else {
                uid=loginBean.getUid();
                saveUser();
                showToast("登陆成功"+uid);
                Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                startActivity(intent);
            }
        }

        @Override
        public void onFail(String msg) {
            showToast("登录失败1");
        }
    };

    @Override
    void initView() {
        setLayout_file(R.layout.activity_login);
        ed_user=(EditText)findViewById(R.id.ed_user);
        ed_pw=(EditText)findViewById(R.id.ed_pw);
        btn_login=(Button)findViewById(R.id.btn_login);
        btn_register=(Button)findViewById(R.id.btn_register);
        checkBox=(CheckBox)findViewById(R.id.checkBox);

        user_phone=getUser_name();
        user_password=getUser_password();

        Boolean remember=getUser_remember();
        if (remember==true){
            ed_user.setText(user_phone);
            ed_pw.setText(user_password);
            checkBox.setChecked(remember);
        }
        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
                startActivityForResult(intent,1);
            }
        });
    }

    @Override
    void initEvent() {
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (ed_user.getText().toString().trim().equals("")||ed_pw.getText().toString().trim().equals("")){
                    showToast("不能为空");
                }
                else{

                    String user_phone=ed_user.getText().toString();
                    String user_password=ed_pw.getText().toString();
                    LoginModel loginModel=new LoginModel();
                    loginModel.getLoginList(user_phone,user_password, loginListener);
                    //拿着用户名和密码去服务器验证

                }
            }
        });
    }

    @Override
    void initData() {

    }
    public void saveUser(){
        SharedPreferences sp=getSharedPreferences("userInfo",MODE_PRIVATE);//用户名存在哪里,存储形式
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("user_phone",ed_user.getText().toString());//以键值的方式储存
        editor.putString("userpass",ed_pw.getText().toString());
        editor.putBoolean("remember",checkBox.isChecked());
        editor.putString("uid",uid);
        editor.commit();//提交
    }
    @Override
    protected void onActivityResult(int requesCode,int resultCode,Intent data){
        super.onActivityResult(requesCode,resultCode,data);
        switch (requesCode){
            case 1:if (resultCode==RESULT_OK){
                String string=data.getStringExtra("users");//user
                Toast.makeText(LoginActivity.this,string,Toast.LENGTH_SHORT).show();
                ed_user.setText(string);
                ed_pw.setText(string);
            }
        }
    }
}
